<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car\CarCost;
use App\Models\Car\ExistingEvidences;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExistingEvidenceController extends Controller
{
    use ResponseData;
    public function index(ExistingEvidences $model,Request $request) {
        $size = $request->input('size',10);
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        if($request->filled('type')){
            $model = $model->where('type','like','%'.$request->type.'%');
        }
        if($request->filled('search')){
            $search = $request->search;
            $model = $model->where(function($query)use($search){
                $query->where('phone','like','%'.$search.'%')
                    ->orWhere('customer_name','like','%'.$search.'%')
                    ->orWhere('order_sn','like','%'.$search.'%');
            });
        }
        $data = $model->paginate($size);
        return $this->success($data);
    }
    public function total(ExistingEvidences $model,Request $request) {
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        if($request->filled('type')){
            $model = $model->where('type','like','%'.$request->type.'%');
        }
        $un_total = $model->sum('unpaid_total');
        $py_total = $model->sum('payment_total');
        return $this->success(compact('un_total','py_total'));
    }
    public function store(ExistingEvidences $model,Request $request){
        $data = $request->all();
        $data['existing_sn'] = $this->getSn();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    //设置订单号
    private function getSn(){
        $car = ExistingEvidences::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($car){
            $num = $car->id+1;
        }else{
            $num = 1;
        }
        return 'cz'.date('Ymd',time()).$num;
    }
    public function edit(ExistingEvidences $model,Request $request){
        if($request->filled('order_sn')){
            $data = $model->where('order_sn',$request->order_id)->first();
        } else {
            $data = $model->find($request->id);
        }
        return $this->success($data);
    }
    public function update(ExistingEvidences $model,Request $request){
        $data = $model->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(ExistingEvidences $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
        return $this->success('删除成功');
    }
}
